package com.base.mine;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.CBaseMvpActivity;
import com.base.commonlib.bean.User;
import com.base.commonlib.event.AddressEvent;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.ToastUtil;
import com.base.mine.config.RouteConfig;
import com.base.mine.databinding.ActivityPhoneloginBinding;
import com.base.mine.presenter.PhoneLoginPresenter;
import com.base.mine.utils.Rotate3D;
import com.base.mine.view.PhoneLoginView;
import com.base.share.ShareLoginEntity;
import com.google.gson.Gson;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by weikailiang on 2019/9/18.
 */

@Route(path = RouteConfig.ACTIVITIY_LOGIN)
public class PhoneLoginActivity extends CBaseMvpActivity<PhoneLoginPresenter> implements PhoneLoginView {
    protected ActivityPhoneloginBinding mBinding;
    Rotate3D anim;
    private UMShareAPI mShareAPI;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_phonelogin;
    }

    @Override
    protected void init() {
        mBinding = (ActivityPhoneloginBinding) mRootBinding;
        mPresenter = new PhoneLoginPresenter(this,this);
        mPresenter.start();
        mShareAPI = UMShareAPI.get(this);
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        mShareAPI.setShareConfig(config);
    }

    @Override
    protected void start() {
        BaseApp.removeActivity(this);
        mBinding.agreeMent.setChecked(true);
        anim = new Rotate3D.Builder(this)
                .bindParentView(mBinding.parentLl)
                .bindPositiveView(mBinding.accountPhoneLl)
                .bindNegativeView(mBinding.accountLoginLl)
                .create();
        mBinding.noPassLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                anim.transform();
                if (anim.isOpen()) {
                    mBinding.noPassLogin.setTextColor(ContextCompat.getColor(mContext,R.color.color_4486FF));
                    mBinding.noPassLogin.setTextSize(18);
                    mBinding.tvReg.setTextColor(ContextCompat.getColor(mContext,R.color.color_tab_text_normal));
                    mBinding.tvReg.setTextSize(16);
                } else {
                    mBinding.noPassLogin.setTextColor(ContextCompat.getColor(mContext,R.color.color_tab_text_normal));
                    mBinding.noPassLogin.setTextSize(16);
                    mBinding.tvReg.setTextColor(ContextCompat.getColor(mContext,R.color.color_4486FF));
                    mBinding.tvReg.setTextSize(18);
                }
            }
        });
        mBinding.agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //同样
                ARouter.getInstance().build("/sweb/common")
                        .withInt("key",1)
                        .withString("url", RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/useragree")
                        .withString("title","江苏中越科技")
                        .withString("name","测试")
                        .navigation();
            }
        });
        mBinding.userAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //协议
                ARouter.getInstance().build("/sweb/common")
                        .withInt("key",1)
                        .withString("url",RealNetWorkUtil.HOST_IP+":8585/kaoshi/index.html#/useragree")
                        .withString("title","江苏中越科技")
                        .withString("name","测试")
                        .navigation();
            }
        });
        mBinding.tvReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                anim.transform();
//                if (anim.isOpen()) {
//                    mBinding.noPassLogin.setTextColor(ContextCompat.getColor(mContext,R.color.color_4486FF));
//                    mBinding.noPassLogin.setTextSize(18);
//                    mBinding.tvReg.setTextColor(ContextCompat.getColor(mContext,R.color.color_tab_text_normal));
//                    mBinding.tvReg.setTextSize(16);
//                } else {
//                    mBinding.noPassLogin.setTextColor(ContextCompat.getColor(mContext,R.color.color_tab_text_normal));
//                    mBinding.noPassLogin.setTextSize(16);
//                    mBinding.tvReg.setTextColor(ContextCompat.getColor(mContext,R.color.color_4486FF));
//                    mBinding.tvReg.setTextSize(18);
//                }
            }
        });
        mBinding.sendCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBinding.sendCode.getText().equals("发送验证码")){
                    if (TextUtils.isEmpty(mBinding.loginInputPhone.getText().toString())){
                        showMes("请输入手机号码");
                        return;
                    }else {
                        mPresenter.SendCode(mBinding.loginInputPhone.getText().toString());
                    }
                }else {
                    showMes("验证码已发送");
                    return;
                }
            }
        });
        mBinding.companyLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mBinding.agreeMent.isChecked()){
                    showMes("请同意注册协议");
                    return;
                }
            }
        });
        mBinding.phoneLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mBinding.agreeMent.isChecked()){
                    showMes("请同意注册协议");
                    return;
                }
                if (TextUtils.isEmpty(mBinding.loginInputPhone.getText().toString())){
                    showMes("请输入手机号码");
                    return;
                }
//                if (!mBinding.loginInputPhone.getText().toString().equals(mPresenter.sendPhone)){
//                    showMes("输入框内手机号和发送验证码手机号不符");
//                    return;
//                }
                if (TextUtils.isEmpty(mBinding.loginInputCode.getText().toString())){
                    showMes("请输入验证码");
                    return;
                }
                if (mBinding.loginInputCode.getText().toString().length()<4){
                    showMes("请输入正确验证码");
                    return;
                }
                mPresenter.phoneLogin(mBinding.loginInputPhone.getText().toString(),mBinding.loginInputCode.getText().toString());
            }
        });
        mBinding.agreeMent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked){
                    mBinding.phoneLogin.setEnabled(false);
                    mBinding.companyLogin.setEnabled(false);
                }else {
                    mBinding.phoneLogin.setEnabled(true);
                    mBinding.companyLogin.setEnabled(true);
                }
            }
        });

        mBinding.forgetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/forgetpw/pw").navigation();
            }
        });
        mBinding.passwordLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,PwdLoginActivity.class);
                startActivity(intent);
               // ARouter.getInstance().build("/mine/pwdlogin").navigation();
            }
        });


        mBinding.loginQq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mShareAPI.isInstall(PhoneLoginActivity.this, SHARE_MEDIA.QQ)) {
                    //第三方QQ登陆
                    mShareAPI.getPlatformInfo(PhoneLoginActivity.this, SHARE_MEDIA.QQ, authListener);
                }else {
                    showMes("请先安装QQ");
                }
            }
        });

        mBinding.loginWechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mShareAPI.isInstall(PhoneLoginActivity.this, SHARE_MEDIA.WEIXIN)) {
                    //第三方微信登陆
                    mShareAPI.getPlatformInfo(PhoneLoginActivity.this, SHARE_MEDIA.WEIXIN, authListener);
                }else {
                    showMes("请先安装微信");
                }
            }
        });
        mBinding.loginWeibo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mShareAPI.isInstall(PhoneLoginActivity.this, SHARE_MEDIA.SINA)) {
                    //第三方微博登录
                    mShareAPI.getPlatformInfo(PhoneLoginActivity.this, SHARE_MEDIA.SINA, authListener);
                }else {
                    showMes("请先安装新浪微博");
                }
            }
        });
        Disposable disposable = RxBus2.getInstance().doSubscribe(User.class, new Consumer<User>() {
            @Override
            public void accept(User user) throws Exception {
                finish();
            }
        });
        RxBus2.getInstance().addSubscription(this, disposable);
    }

    UMAuthListener authListener = new UMAuthListener() {
        /**
         * @desc 授权开始的回调
         * @param platform 平台名称
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @desc 授权成功的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param data 用户资料返回
         */
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            ShareLoginEntity loginEntity = new Gson().fromJson(new Gson().toJson(data),ShareLoginEntity.class);
            switch (platform){
                case QQ:
                    showMes("QQ授权成功：ID="+loginEntity.getUid()+"\n姓名："+loginEntity.getName()+"\n性别："+loginEntity.getGender());
                    break;
                case WEIXIN:
                    showMes("微信授权成功：ID="+loginEntity.getUid()+"\n姓名："+loginEntity.getName()+"\n性别："+loginEntity.getGender());
                    break;
                case SINA:
                    showMes("新浪微博授权成功：ID="+loginEntity.getUid()+"\n姓名："+loginEntity.getName()+"\n性别："+loginEntity.getGender());
                    break;
            }


        }

        /**
         * @desc 授权失败的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            showMes("授权失败");
        }

        /**
         * @desc 授权取消的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         */
        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            showMes("取消授权");
        }
    };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.release();
        mShareAPI.release();
    }

    @Override
    public TextView returnSendCodeView() {
        return mBinding.sendCode;
    }
}
